﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace GTA
{
    public enum PlatformType
    {
        FLURRY = 0,
        APPSFLYER = 1,
        FIREBASE = 2,
        COUNTLY = 3,
        FACEBOOK = 4,
        ADJUST = 5,
        SENSORSBI = 6,
    }

    public class AnalyticsHelper
    {
        public static void LogEvent(string platforms, string name, Dictionary<string, object> param)
        {
            string[] platform_list = platforms.Split(';');
            PlatformType[] platforms_type = new PlatformType[platform_list.Length];
            for (int i = 0; i < platform_list.Length; ++i)
            {
                string str = platform_list[i];
                str = str.ToUpper();
                if (str.Equals(PlatformType.FLURRY.ToString()))
                {
                    platforms_type[i] = PlatformType.FLURRY;
                }
                else if (str.Equals(PlatformType.APPSFLYER.ToString()))
                {
                    platforms_type[i] = PlatformType.ADJUST;
                }
                else if (str.Equals(PlatformType.ADJUST.ToString()))
                {
                    platforms_type[i] = PlatformType.ADJUST;
                }
                else if (str.Equals(PlatformType.FIREBASE.ToString()))
                {
                    platforms_type[i] = PlatformType.FIREBASE;
                }
                else if (str.Equals(PlatformType.COUNTLY.ToString()))
                {
                    platforms_type[i] = PlatformType.COUNTLY;
                }
                else if (str.Equals(PlatformType.FACEBOOK.ToString()))
                {
                    platforms_type[i] = PlatformType.FACEBOOK;
                }
                else if (str.Equals(PlatformType.SENSORSBI.ToString()))
                {
                    platforms_type[i] = PlatformType.SENSORSBI;
                }
                else
                {
                    GameLogger.Log("找不到匹配的平台 [" + str + "]");
                }
            }

            LogEvent(platforms_type, name, param);
        }

        private static void LogEvent(PlatformType[] platforms, string name, Dictionary<string, object> param)
        {
            GameLogger.Log("@Analytics dispatchLogEvent - [" + name + "] - [" + platforms[0] + "] - " +
                           Utils.ParseToString(param));
            if (Application.platform == RuntimePlatform.Android ||
                Application.platform == RuntimePlatform.IPhonePlayer)
            {
                for (int i = 0; i < platforms.Length; i++)
                {
                    dispatchLogEvent(platforms[i], name, param);
                }
            }
            else
            {
                for (int i = 0; i < platforms.Length; i++)
                {
/*                    if (platforms[i] == PlatformType.COUNTLY)
                    {
                        try
                        {
                            GameLogger.Log(MotionFramework.Console.GameLogType.CountlyLog,
                                $"eventId:{param["en"]} \teventName:{name}");
                        }
                        catch (Exception e)
                        {
                            Debug.Log(e.ToString());
                        }
                    }
                    else */
                    if (platforms[i] == PlatformType.FIREBASE)
                    {
                        try
                        {
                            GameLogger.Log(MotionFramework.Console.GameLogType.FireLog, $"eventName:{name}");
                        }
                        catch (Exception e)
                        {
                            Debug.Log(e.ToString());
                        }
                    }
                    else if (platforms[i] == PlatformType.FACEBOOK)
                    {
                        try
                        {
                            GameLogger.Log(MotionFramework.Console.GameLogType.FBLog, $"eventName:{name}");
                        }
                        catch (Exception e)
                        {
                            Debug.Log(e.ToString());
                        }
                    }
                }
            }
        }

        private static void dispatchLogEvent(PlatformType platform, string name, Dictionary<string, object> param)
        {
            // Debug.Log("@Analytics dispatchLogEvent - 1"+platform+" - "+name+" - "+param.Keys.Count);

            switch (platform)
            {
/*                case PlatformType.FLURRY:
                {
                    if (param == null)
                    {
                        // FlurryWrapper.Instance.LogEvent(name);
                    }
                    else
                    {
                        Dictionary<string, string> newParam = new Dictionary<string, string>();
                        foreach (var kvp in param)
                        {
                            newParam.Add(kvp.Key, kvp.Value.ToString());
                        }
                        // FlurryWrapper.Instance.LogEvent(name, newParam);
                    }
                }
                    break;*/
                case PlatformType.APPSFLYER:
                {
                    if (param != null)
                    {
                        Dictionary<string, string> newParam = new Dictionary<string, string>();
                        foreach (var kvp in param)
                        {
                            newParam.Add(kvp.Key, kvp.Value.ToString());
                        }

                        AdjustWrapper.Instance.LogEvent(name, newParam);
                    }
                    else
                    {
                        AdjustWrapper.Instance.LogEvent(name, null);
                    }
                }
                    break;
                case PlatformType.ADJUST:
                {
                    if (param != null)
                    {
                        Dictionary<string, string> newParam = new Dictionary<string, string>();
                        foreach (var kvp in param)
                        {
                            newParam.Add(kvp.Key, kvp.Value.ToString());
                        }
                        AdjustWrapper.Instance.LogEvent(name, newParam);
                    }
                    else
                    {
                        AdjustWrapper.Instance.LogEvent(name, null);
                    }
                }
                    break;
                case PlatformType.FIREBASE:
                {

                    LogEvent(name, MotionFramework.Console.GameLogType.FireLog, param);
                    // GameLogger.Log(MotionFramework.Console.GameLogType.FireLog, $"eventName:{name}");
                }
                    break;
/*                case PlatformType.COUNTLY:
                {
                    //Debug.Log("shengming AnalyticsHelper id =" + param["en"]);

                    CountlyHelpWrapper.Instance.LogEvent(name, param);
                }
                    break;*/
                case PlatformType.FACEBOOK:
                {

                    GameLogger.Log(MotionFramework.Console.GameLogType.FBLog, $"eventName:{name}");
                }
                    break;
                case PlatformType.SENSORSBI:
                    {
                        //SenSorsBIWrapper.Instance.LogEvent(name, param);
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dic"></param>
        public static void LogEvent(string name, MotionFramework.Console.GameLogType gameLogType
            , Dictionary<string, object> dictPrivate)
        {
            string t = "";
            if (dictPrivate != null)
            {
                foreach (var item in dictPrivate)
                {
                    t += $"\r\n\t{item.Key}:{item.Value}";
                }
            }

            t = $"[{name}]   [{DateTime.Now.ToString()}] {t}";

            GameLogger.Log(gameLogType, t);
        }

        public static void AdjustLogEventRevenue(string eventName, Dictionary<string, string> eventValues, double revenue,
            string currency)
        {
            AdjustWrapper.Instance.LogEventRevenue(eventName,eventValues,revenue,currency);
        }
    }
}